package com.cheetah.rest;

import com.cheetah.builder.Result;
import com.cheetah.builder.ResultBuilder;
import com.cheetah.dto.OrgDTO;
import com.cheetah.exceptions.CheetahRuntimeException;
import com.cheetah.exceptions.UserError;
import com.cheetah.service.OrgService;
import com.cheetah.vo.OrgVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @Author chrishoo
 * @Date 2020/7/23
 * @Version V1.0
 * @Description: 组织架构相关
 **/
@RestController
@RequestMapping("/org")
public class OrgRest {

    @Autowired
    private OrgService orgService;

    @PostMapping("/add")
    public Result<OrgDTO> add(OrgVO orgVO){

        Optional.ofNullable(orgVO.getParent()).orElseThrow(() -> new CheetahRuntimeException(UserError.ERROR_CODE_200202));
        Optional.ofNullable(orgVO.getName()).orElseThrow(() -> new CheetahRuntimeException(UserError.ERROR_CODE_200203));
        Optional.ofNullable(orgVO.getNetworkId()).orElseThrow(() -> new CheetahRuntimeException(UserError.ERROR_CODE_200203));

        return ResultBuilder.builder().success(orgService.save(orgVO));
    }

    @PostMapping("/update")
    public Result update(OrgVO orgVO){
        //TODO 增加健壮性校验
        orgService.update(orgVO);
        return ResultBuilder.builder().success();
    }

    @PostMapping("/delete")
    public Result delete(OrgVO orgVO){
        //TODO 增加健壮性校验
        orgService.delete(orgVO.getId());
        return ResultBuilder.builder().success();
    }
}
